package com.lindenvalley.ch.parser;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

import com.lindenvalley.ch.content.Task;
import com.lindenvalley.ch.content.Week;
import com.lindenvalley.ch.content.Weeks;
import com.lindenvalley.ch.network.WorkServer;

public class TaskXmlParser extends AbstractXmlParser{
	
	private static final String TASK_TAG = "task";
	private static final String TASKS_TAG = "tasks";
	private static final String WEEK_TAG = "week";
	private static final String WEEK_YEAR_ATTRIBUTE = "year";	
	private static final String WEEK_INDEX_ATTRIBUTE = "index";
	private static final String WEEK_INDEX_START_ATTRIBUTE = "indexStart";
	private static final String WEEK_START_DATE_ATTRIBUTE = "startDate";
	private static final String WEEK_END_DATE_ATTRIBUTE = "endDate";

	public TaskXmlParser() {
		super();		
	}
	
	public Weeks parse(String content){
				
		Task task = null;
		Weeks weeks = new Weeks();
		Week week = null;
		
		if (content != null && content.length() > 0 && !WorkServer.DENY_VALUE.equals(content)){
//			Log.i("XmlParser", content);
			content = decodeFromHtml(content);
			XmlPullParserFactory factory;
			try {
				factory = XmlPullParserFactory.newInstance();
				factory.setNamespaceAware(true);
				XmlPullParser xpp = factory.newPullParser();
				xpp.setInput(new StringReader(content));
				int eventType = xpp.getEventType();
				String value;
				Method method = null;
		        while (eventType != XmlPullParser.END_DOCUMENT) {
		        	switch (eventType){		        	
		        	case XmlPullParser.START_TAG:		        		
		        		value = xpp.getName();
		        		if (TASKS_TAG.equals(value)){
		        			int year = Integer.decode(xpp.getAttributeValue(null, WEEK_YEAR_ATTRIBUTE));
		        			weeks.setYear(year);
		        		} else if (WEEK_TAG.equals(value)){
		        			String index = xpp.getAttributeValue(null, WEEK_INDEX_ATTRIBUTE);
		        			String indexStart = xpp.getAttributeValue(null, WEEK_INDEX_START_ATTRIBUTE);
		        			String startDate = xpp.getAttributeValue(null, WEEK_START_DATE_ATTRIBUTE);
		        			String endDate = xpp.getAttributeValue(null, WEEK_END_DATE_ATTRIBUTE);
		        			week = new Week();
//		        			week.setIndex(Byte.decode(index));
//		        			week.setIndexStart(Byte.decode(indexStart));
//		        			week.setStartDate(startDate);
//		        			week.setEndDate(endDate);
		        		} else if (TASK_TAG.equals(value)){
		        			task = new Task();		        			
		        		} else {
		        			try{
			        			if (task != null)
			        				method = task.getClass().getDeclaredMethod(SET + value, String.class);
			        		} catch (NoSuchMethodException e) {
			    				// TODO Auto-generated catch block
//			    				e.printStackTrace();
			    			}
		        		}
		        		break;
		        	case XmlPullParser.END_TAG:
		        		value = xpp.getName();
		        		if (TASK_TAG.equals(value) && week != null){
		        			week.addTask(task);
		        		} else if (WEEK_TAG.equals(value) && week != null){
//	        				weeks.addWeek(week.getIndex(), week);
		        		}
		        		method = null;
		        		break;
		        	case XmlPullParser.TEXT:
		        		if (method != null){
			        		value = xpp.getText();
			        		method.setAccessible(true);
			        		method.invoke(task, value);
		        		}
		        		break;
		        	
		        	}
		        	eventType = xpp.next();
		        	
		        }		        

			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        
		}
		return weeks;

	}

}
